<template>
  <div class="app-container">
    <page-tools
      search-txt="标签名称"
      add-txt="新增标签"
      @onAdd="onAddOrEdit(0)"
    />
    <el-table :data="list" border style="width: 100%">
      <el-table-column
        fixed
        type="index"
        label="序列"
        width="50"
        align="center"
      />
      <el-table-column prop="name" label="标签名" align="center" />
      <el-table-column prop="description" label="备注" align="center" />
      <el-table-column prop="article_count" label="文章数量" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center">
        <template v-slot="scope">
          {{ scope.row.createTime | getTime }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            :disabled="!checkPermission('point-update')"
            @click="onAddOrEdit(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
            :disabled="!checkPermission('point-del')"
            class="del"
            @click="onDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <PagePagination
      :page="pageParams.page"
      :page-size="pageParams.pageSize"
      :total="total"
      @updata-page="onUpdataPage"
    />

    <el-dialog :visible.sync="dialogVisible" :title="title + '标签'" center>
      <el-form ref="formRef" label-width="100px" :model="form" :rules="tagRules">
        <el-form-item label="标签名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTagListAPI,
  getTagByIdAPI,
  addTagAPI,
  putTagAPI,
  delTagByIdAPI
} from '@/api/tag'
import { tagRules } from '@/utils/rules'

export default {
  data() {
    return {
      formRef: '',
      tagRules,
      list: [],
      pageParams: {
        page: 1,
        pageSize: 5
      },
      total: 0,
      dialogVisible: false,
      form: {},
      title: ''
    }
  },
  mounted() {
    this.getTagList()
  },
  methods: {
    async getTagList() {
      const res = await getTagListAPI()
      this.list = res.data.result
      this.total = res.data.total
    },
    onUpdataPage(page) {
      this.pageParams.page = page
      this.getTagList()
    },
    async onAddOrEdit(row) {
      this.dialogVisible = true
      if (row.id) {
        const res = await getTagByIdAPI(row.id)
        this.form = res.data
        this.title = '编辑'
      } else {
        this.title = '新增'
      }
    },
    async onSubmit() {
      this.$refs['formRef'].validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await putTagAPI(this.form.id, this.form)
            this.getTagList()
            this.$message({
              message: '编辑标签成功',
              type: 'success'
            })
          } else {
            await addTagAPI(this.form)
            this.getTagList()
            this.$message({
              message: '新增标签成功',
              type: 'success'
            })
          }
          this.dialogVisible = false
        } else {
          this.$message.error('请填写完整信息')
          return false
        }
      })
    },
    onClose() {
      this.dialogVisible = false
      this.form = {}
      this.$refs['formRef'].clearValidate()
    },
    onDel(row) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delTagByIdAPI(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getTagList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped></style>
